Imports System.Windows.Forms
Imports Vintasoft.Imaging.Codecs
Imports Vintasoft.Imaging

Namespace Imaging.Codecs.Dialogs
	Public Partial Class PdfEncoderSettingsForm
		Inherits Form

		#Region "Fields"

		Private _jpeg2000IsAvailable As Boolean

		#End Region



		#Region "Constructors"

		Public Sub New()
			InitializeComponent()
			If Not AvailableEncoders.IsEncoderAvailable("Jbig2") Then
				compressionJbig2RadioButton.Enabled = False
			End If
			_jpeg2000IsAvailable = AvailableEncoders.IsEncoderAvailable("Jpeg2000")
			If Not _jpeg2000IsAvailable Then
				compressionJpeg2000RadioButton.Enabled = False
			End If
			EditAnnotationSettings = False
		End Sub

		#End Region



		#Region "Properties"

		Private _encoderSettings As PdfEncoderSettings
		''' <summary>
		''' Gets or sets PDF encoder settings.
		''' </summary>
		Public Property EncoderSettings() As PdfEncoderSettings
			Get
				Return _encoderSettings
			End Get
			Set
				If value Is Nothing Then
					Throw New ArgumentOutOfRangeException()
				End If
				If _encoderSettings IsNot value Then
					_encoderSettings = value
					ShowEncoderSettings()
				End If
			End Set
		End Property

		''' <summary>
		''' Determines that existing document should be append.
		''' </summary>
		Public Property AppendExistingDocument() As Boolean
			Get
				Return appendCheckBox.Checked
			End Get
			Set
				appendCheckBox.Checked = value
			End Set
		End Property

		''' <summary>
		''' Gets or sets a value indicating whether <see cref="AppendExistingDocument"/> is enabled.
		''' </summary>
		Public Property AppendExistingDocumentEnabled() As Boolean
			Get
				Return appendCheckBox.Enabled
			End Get
			Set
				appendCheckBox.Enabled = value
			End Set
		End Property

		Public Property EditAnnotationSettings() As Boolean
			Get
				Return tabControl1.TabPages.Contains(annotationsTabPage)
			End Get
			Set
				If EditAnnotationSettings <> value Then
					If value Then
						tabControl1.TabPages.Add(annotationsTabPage)
					Else
						tabControl1.TabPages.Remove(annotationsTabPage)
					End If
				End If
			End Set
		End Property

		Private _formatOfAnnotations As AnnotationsFormat
		Public Property FormatOfAnnotations() As AnnotationsFormat
			Get
				Return _formatOfAnnotations
			End Get
			Set
				_formatOfAnnotations = value
				annotationsBinaryCheckBox.Checked = (_formatOfAnnotations And AnnotationsFormat.VintasoftBinary) <> 0
				annotationXmpCheckBox.Checked = (_formatOfAnnotations And AnnotationsFormat.VintasoftXmp) <> 0
			End Set
		End Property

		Public Property GeneratePdfAnnotationsAppearence() As Boolean
			Get
				Return annotationsPdfAppearanceCheckBox.Checked
			End Get
			Set
				annotationsPdfAppearanceCheckBox.Checked = value
			End Set
		End Property

		#End Region



		#Region "Methods"

		Protected Overrides Sub OnLoad(e As EventArgs)
			MyBase.OnLoad(e)

			If EncoderSettings Is Nothing Then
				EncoderSettings = New PdfEncoderSettings()
			End If
		End Sub

		Protected Overrides Sub OnShown(e As EventArgs)
			MyBase.OnShown(e)

			If EditAnnotationSettings Then
				tabControl1.TabPages.Remove(annotationsTabPage)
				tabControl1.TabPages.Insert(1, annotationsTabPage)
			End If
		End Sub

		Private Sub compressionRadioButton_CheckedChanged(sender As Object, e As EventArgs)
			pdfaCheckBox.Enabled = Not appendCheckBox.Checked

			jpeg2000CompressionSettingsGroupBox.Visible = False
			jpegCompressionSettingsGroupBox.Visible = False
			jbig2CompressionSettingsGroupBox.Visible = False
			If compressionJpegRadioButton.Checked OrElse compressionAutoRadioButton.Checked Then
				jpegCompressionSettingsGroupBox.Visible = True
			ElseIf compressionJpeg2000RadioButton.Checked Then
				jpeg2000CompressionSettingsGroupBox.Visible = True
				pdfaCheckBox.Checked = False
				pdfaCheckBox.Enabled = False
			ElseIf compressionJbig2RadioButton.Checked Then
				jbig2CompressionSettingsGroupBox.Visible = True
			ElseIf compressionLzwRadioButton.Checked Then
				pdfaCheckBox.Checked = False
				pdfaCheckBox.Enabled = False
			End If

		End Sub

		Private Sub appendCheckBox_CheckedChanged(sender As Object, e As EventArgs)
			pdfaCheckBox.Enabled = Not appendCheckBox.Checked
			If appendCheckBox.Checked Then
				pdfaCheckBox.Checked = False
			End If
		End Sub

		Private Sub buttonCancel_Click(sender As Object, e As EventArgs)
			DialogResult = DialogResult.Cancel
		End Sub

		Private Sub pdfaCheckBox_CheckedChanged(sender As Object, e As EventArgs)
			compressionJpeg2000RadioButton.Enabled = _jpeg2000IsAvailable AndAlso Not pdfaCheckBox.Checked
			compressionLzwRadioButton.Enabled = Not pdfaCheckBox.Checked
		End Sub


		Private Sub jpeg2000SettingsButton_Click(sender As Object, e As EventArgs)
			Dim jpeg2000SettingsDialog As New Jpeg2000EncoderSettingsForm()
			jpeg2000SettingsDialog.EncoderSettings = EncoderSettings.Jpeg2000Settings
			jpeg2000SettingsDialog.ShowDialog()
		End Sub

		Private Sub jbig2SettingsButton_Click(sender As Object, e As EventArgs)
			Dim jbig2SettingsDialog As New Jbig2EncoderSettingsForm()
			jbig2SettingsDialog.EncoderSettings = EncoderSettings.Jbig2Settings
			jbig2SettingsDialog.AppendExistingDocumentEnabled = False
			jbig2SettingsDialog.ShowDialog()
		End Sub

		Private Sub buttonOk_Click(sender As Object, e As EventArgs)
			SetEncoderSettings()

			DialogResult = DialogResult.OK
		End Sub


		Private Sub ShowEncoderSettings()
			pdfaCheckBox.Checked = _encoderSettings.PdfACompatible

			jbig2UseGlobalsCheckBox.Checked = _encoderSettings.Jbig2UseGlobals
			jpegQualityNumericUpDown.Value = _encoderSettings.JpegQuality
			jpegGrayscaleCheckBox.Checked = _encoderSettings.JpegSaveAsGrayscale

			titleTextBox.Text = _encoderSettings.DocumentTitle
			authorTextBox.Text = _encoderSettings.DocumentAuthor
			creatorTextBox.Text = _encoderSettings.DocumentCreator
			keywordsTextBox.Text = _encoderSettings.DocumentKeywords
			producerTextBox.Text = _encoderSettings.DocumentProducer
			subjectTextBox.Text = _encoderSettings.DocumentSubject

			Select Case _encoderSettings.Compression
				Case PdfImageCompression.Auto
					compressionAutoRadioButton.Checked = True
					Exit Select
				Case PdfImageCompression.CcittFax
					compressionCcittRadioButton.Checked = True
					Exit Select
				Case PdfImageCompression.Jbig2
					If compressionJbig2RadioButton.Enabled Then
						compressionJbig2RadioButton.Checked = True
					End If
					Exit Select
				Case PdfImageCompression.Jpeg
					compressionJpegRadioButton.Checked = True
					Exit Select
				Case PdfImageCompression.Jpeg2000
					If compressionJpeg2000RadioButton.Enabled Then
						compressionJpeg2000RadioButton.Checked = True
					End If
					Exit Select
				Case PdfImageCompression.Lzw
					compressionLzwRadioButton.Checked = True
					Exit Select
				Case PdfImageCompression.None
					compressionNoneRadioButton.Checked = True
					Exit Select
				Case PdfImageCompression.Zip
					compressionZipRadioButton.Checked = True
					Exit Select
			End Select

			annotationsPdfAppearanceCheckBox.Checked = _encoderSettings.GenerateAnnotationAppearance
		End Sub

		Private Sub SetEncoderSettings()
			EncoderSettings.PdfACompatible = pdfaCheckBox.Checked

			EncoderSettings.Jbig2UseGlobals = jbig2UseGlobalsCheckBox.Checked
			EncoderSettings.JpegQuality = CInt(Math.Truncate(jpegQualityNumericUpDown.Value))
			EncoderSettings.JpegSaveAsGrayscale = jpegGrayscaleCheckBox.Checked

			_encoderSettings.DocumentTitle = titleTextBox.Text
			_encoderSettings.DocumentAuthor = authorTextBox.Text
			_encoderSettings.DocumentCreator = creatorTextBox.Text
			_encoderSettings.DocumentKeywords = keywordsTextBox.Text
			_encoderSettings.DocumentProducer = producerTextBox.Text
			_encoderSettings.DocumentSubject = subjectTextBox.Text

			If compressionAutoRadioButton.Checked Then
				EncoderSettings.Compression = PdfImageCompression.Auto
			ElseIf compressionCcittRadioButton.Checked Then
				EncoderSettings.Compression = PdfImageCompression.CcittFax
			ElseIf compressionJbig2RadioButton.Checked Then
				EncoderSettings.Compression = PdfImageCompression.Jbig2
			ElseIf compressionJpeg2000RadioButton.Checked Then
				EncoderSettings.Compression = PdfImageCompression.Jpeg2000
			ElseIf compressionJpegRadioButton.Checked Then
				EncoderSettings.Compression = PdfImageCompression.Jpeg
			ElseIf compressionLzwRadioButton.Checked Then
				EncoderSettings.Compression = PdfImageCompression.Lzw
			ElseIf compressionNoneRadioButton.Checked Then
				EncoderSettings.Compression = PdfImageCompression.None
			ElseIf compressionZipRadioButton.Checked Then
				EncoderSettings.Compression = PdfImageCompression.Zip
			End If

			If EditAnnotationSettings Then
				_formatOfAnnotations = AnnotationsFormat.None
				If annotationsBinaryCheckBox.Checked Then
					_formatOfAnnotations = _formatOfAnnotations Or AnnotationsFormat.VintasoftBinary
				End If
				If annotationXmpCheckBox.Checked Then
					_formatOfAnnotations = _formatOfAnnotations Or AnnotationsFormat.VintasoftXmp
				End If
			End If

			EncoderSettings.GenerateAnnotationAppearance = annotationsPdfAppearanceCheckBox.Checked
		End Sub

		#End Region

	End Class
End Namespace
